package com.naiking.manager
{
	import com.naiking.data.HashMap;
	
	import flash.events.*;
	
	public class EventManager {
		
		private static var _evtMap:HashMap = new HashMap();
		
		public function EventManager(){
			super();
			throw (new Event("静态类"));
		}
		public static function dispatchEvent(e:Event):void{
			var list:Vector.<Function>;
			var fun:Function;
			var listenerList:Vector.<Function> = _evtMap.getValue(e.type);
			if (listenerList){
				list = new Vector.<Function>();
				list = list.concat(listenerList);
				for each (fun in list) {
					if (fun != null){
						fun(e);
					};
				};
			};
		}
		public static function addEvent(type:String, listener:Function):void{
			var listenerList:Vector.<Function>;
			if (!(hasEvent(type, listener))){
				listenerList = _evtMap.getValue(type);
				if (!(listenerList)){
					listenerList = new Vector.<Function>();
					_evtMap.add(type, listenerList);
				};
				listenerList[listenerList.length] = listener;
			} else {
				trace((("重复注册相同事件:" + type) + ",返回!"));
			};
		}
		public static function removeEvent(type:String, listener:Function):void{
			var fun:Function;
			var listenerList:Vector.<Function> = _evtMap.getValue(type);
			var cnt:uint;
			if (listenerList){
				for each (fun in listenerList) {
					if (listener == fun){
						listenerList.splice(cnt, 1);
					};
					cnt++;
				};
				if (listenerList.length){
					_evtMap.add(type, listenerList);
				} else {
					_evtMap.remove(type);
				};
			};
		}
		public static function hasTypeEvent(type:String):Boolean{
			return (_evtMap.containsKey(type));
		}
		public static function hasEvent(type:String, listener:Function):Boolean{
			var index:int;
			var listenerList:Vector.<Function> = _evtMap.getValue(type);
			if (listenerList){
				index = listenerList.indexOf(listener);
				if (index != -1){
					return (true);
				};
			};
			return (false);
		}
		public static function getEventsNum():int{
			return (_evtMap.length);
		}
		public static function removeAllEvents():void{
			_evtMap.clear();
		}
		
	}
}//package com.rpgGame.core.manager 